package org.myblog.myblogserver.code.service.impl;

import org.myblog.myblogserver.code.entity.User;
import org.myblog.myblogserver.code.mapper.UserMapper;
import org.myblog.myblogserver.code.service.UserService;
import org.myblog.myblogserver.config.properties.JwtProperties;
import org.myblog.myblogserver.dao.UserLoginDao;
import org.myblog.myblogserver.util.JwtUtil;
import org.myblog.myblogserver.util.SendCode;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.myblog.myblogserver.code.entity.User;
import org.myblog.myblogserver.code.mapper.UserMapper;
import org.myblog.myblogserver.code.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.myblog.myblogserver.config.GlobalConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
    @Autowired
    private StringRedisTemplate stringRedisTemplateTemplate;

    @Autowired
    private JwtProperties jwtProperties;


    public UserLoginDao login(User user, String code) {
        if(user.getPassword()==null){
            String redis_code=stringRedisTemplateTemplate.opsForValue().get(GlobalConfig.CODETEXT+user.getEmail());
            System.out.println("redis_code.equals(code) = " + redis_code.equals(code));
            if(redis_code.equals(code)){
                User re_user =getOne(new QueryWrapper<User>().eq("email", user.getEmail()));
                if(re_user==null){
                    re_user=new User();
                    String uuidStr="";
                    while (true){
                        uuidStr=UUID.randomUUID().toString().replace("-", "");
                        if(getById(uuidStr)==null){
                            break;
                        }
                    }
                    re_user.setId(uuidStr);
                    re_user.setEmail(user.getEmail());
                    String na= SendCode.getCode(4);
                    re_user.setUsername("xiaoxixi"+na);
                    String pass= SendCode.getCode(8);
                    re_user.setPassword(pass);
                    re_user.setState(null);

                    save(re_user);
                }
                UserLoginDao userLoginDao=generateJwt(user);
                return userLoginDao;
            }
            return null;
        }else {
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("email", user.getEmail());
            queryWrapper.eq("password", user.getPassword());
            User userInfo= getOne(queryWrapper);
            if (userInfo==null){
                return null;
            }
            UserLoginDao userLoginDao=generateJwt(userInfo);
            return userLoginDao;
        }
    }


    public Boolean sign(User user, String code) {
        String email = user.getEmail();

        String redis_code=stringRedisTemplateTemplate.opsForValue().get(GlobalConfig.CODETEXT+email);
        if(redis_code.equals(code)){
            String uuidStr="";
            while(true){
                UUID uuid= UUID.randomUUID();
                uuidStr = uuid.toString().replace("-", "");
                if(getById(uuidStr)==null){
                    break;
                }
            }
            user.setId(uuidStr);
            System.out.println("user = " + user);
            return save(user);
        }
        return false;
    }



    /**
     * 生成jwt令牌专用方法
     *
     * @param userInfo
     * @return
     */
    protected UserLoginDao generateJwt(User userInfo) {
        //权限验证成功，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();

        claims.put(GlobalConfig.USER_ID, userInfo.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);

        UserLoginDao userLoginDao = UserLoginDao.builder()
                .id(userInfo.getId())
                .userName(userInfo.getUsername())
                .token(token)
                .build();
        return userLoginDao;
    }
}